//
//  Adapter.cpp
//  Archer
//
//  Created by wangdi on 12-10-10.
//
//

#include "Adapter.h"

using namespace cocos2d;

namespace storm
{

	Adapter *Adapter::Instance()
	{
		static Adapter adapter;
		return &adapter;
	}

	void Adapter::Init(cocos2d::CCSize sizeWorldDesign, float resScale, bool bHD)
	{
		m_bHD         = bHD;
		m_sizeDesign   = sizeWorldDesign;

		m_sizeScreen      = cocos2d::CCDirector::sharedDirector()->getWinSize();
		m_sizeScreenPixel = cocos2d::CCDirector::sharedDirector()->getWinSizeInPixels();

		m_hdScale = 1.0f;

		if(bHD)
		{
			if(cocos2d::CCDirector::sharedDirector()->getWinSize().height > 1136)
			{
				m_hdScale = 2.0f;
			}
			else if(cocos2d::CCDirector::sharedDirector()->getWinSize().height < 960)
			{
				m_hdScale = 0.5f;
			}
		}

		// 2.0.4没有高清模式了。。


		m_resScale = resScale;

		m_posScale = m_resScale;
	}



	void Adapter::EnableAdjustScreen(cocos2d::CCSize sizeBase)
	{
		m_xScale = sizeBase.width  / m_sizeDesign.width;
		m_yScale = sizeBase.height / m_sizeDesign.height;
	}

	void Adapter::EnableUserAdjust(cocos2d::CCPoint ptAdjust, float userScale)
	{
		m_userScale = userScale;
		m_ptAdjust  = ptAdjust;
	}

	void Adapter::EnableRetainaAdjust()
	{

	}

	void Adapter::AdjustLayer(cocos2d::CCLayer *pLayer)
	{
		pLayer->setAnchorPoint(ccp(0.5, 0.5));
		pLayer->ignoreAnchorPointForPosition(false);
		pLayer->setContentSize(CCSizeMake(m_sizeDesign.width, m_sizeDesign.height));
		pLayer->setScaleX(m_userScale * m_xScale * m_hdScale);
		pLayer->setScaleY(m_userScale * m_yScale * m_hdScale);
		CCSize s = CCDirector::sharedDirector()->getWinSize();

		pLayer->setPosition(ccp(s.width*0.5f + m_ptAdjust.x, s.height*0.5f + m_ptAdjust.y));
	}

	CCPoint Adapter::AdjustRes(const CCPoint &pt)
	{

		return ccpMult(pt, m_posScale);
	}

	CCSize Adapter::AdjustRes(const CCSize &s)
	{
		return CCSizeMake(s.width * m_posScale, s.height * m_posScale);
	}

	CCPoint Adapter::AdjustScreen(const cocos2d::CCPoint &pt)
	{
		float xDiff = pt.x - m_sizeDesign.width*0.5f;
		float yDiff = pt.y - m_sizeDesign.height*0.5f;

		xDiff *= m_sizeScreenPixel.width  / m_sizeDesign.width  / m_userScale;
		yDiff *= m_sizeScreenPixel.height / m_sizeDesign.height / m_userScale;


		return ccpAdjustRes(m_sizeDesign.width*0.5f + xDiff, m_sizeDesign.height*0.5f + yDiff);
	}

	CCRect Adapter::AdjustRes(const cocos2d::CCRect &rc)
	{
		return CCRect(m_posScale * rc.origin.x ,
			m_posScale * rc.origin.y ,
			m_posScale * rc.size.width ,
			m_posScale * rc.size.height);
	}

	float Adapter::AdjustRes(float f)
	{
		return m_posScale * f;
	}

	CCPoint Adapter::RestoreRes(const cocos2d::CCPoint &pt)
	{
		return CCPoint(pt.x / m_posScale, pt.y / m_posScale);
	}

	cocos2d::CCSize Adapter::RestoreRes(const cocos2d::CCSize &s)
	{
		return CCSizeMake(s.width / m_posScale, s.height / m_posScale);
	}

	cocos2d::CCRect Adapter::RestoreRes(const cocos2d::CCRect &rc)
	{
		return CCRect(rc.origin.x / m_posScale,
			rc.origin.y / m_posScale,
			rc.size.width / m_posScale,
			rc.size.height/ m_posScale);
	}

	float Adapter::RestoreRes(float f)
	{
		return f/m_posScale;
	}

	float Adapter::AdjustHD(float f)
	{
		return f / m_hdScale;
	}

	float Adapter::RestoreHD(float f)
	{
		return f * m_hdScale;
	}

	cocos2d::CCPoint Adapter::ScreenCenter()
	{
		return CCPointMake(m_sizeScreen.width*0.5f, m_sizeScreen.height*0.5f);
	}

}
